<!DOCTYPE>
<html>
  <head>
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<!--导入Google Maps API库文件。注意将本代码中的API Key替换为前文申请到的API Key-->
<!--script src="http://maps.google.com/maps?file=api&amplanguage=jp&amp;v=2&amp;key=ABQIAAAAZ8Z5TVfbQekbozd5k_-PVxRrOsJ3EATJHXekESPTdZ8qEdXJTRT-NyFBJTc38QocmhMWDUzMHLqRlg" type="text/javascript"-->
<!--/script -->
<!-- 使用API V3 -->
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript">

//<![CDATA[
<!--    var map;-->
var geocoder;
var map;

	//计算时间差；
	Date.prototype.dateDiff = function(interval,objDate){
    //若參數不足或 objDate 不是日期物件則回傳 undefined
    if(arguments.length<2||objDate.constructor!=Date) return undefined;
    switch (interval) {
      //計算秒差
      case "s":return parseInt((objDate-this)/1000);
      //計算分差
      case "n":return parseInt((objDate-this)/60000);
      //計算時差
      case "h":return parseInt((objDate-this)/3600000);
      //計算日差
      case "d":return parseInt((objDate-this)/86400000);
      //計算週差
      case "w":return parseInt((objDate-this)/(86400000*7));
      //計算月差
      case "m":return (objDate.getMonth()+1)+((objDate.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
      //計算年差
      case "y":return objDate.getFullYear()-this.getFullYear();
      //輸入有誤
      default:return undefined;
    }
  }
	
<!--    //地理编码查询地点经纬度-->
<!--    function locate(address)-->
<!--    {-->
<!--        var geocoder=new GClientGeocoder();-->
<!--        var oldtime=new Date();-->
<!--        geocoder.getLatLng(address,function(point)-->
<!--          {-->
<!--        	var newtime=new Date();-->
<!--        	var secdiff=oldtime.dateDiff("s",newtime);-->
<!--            if(!point)-->
<!--            {-->
<!--              alert(address+" not found");-->
<!--              alert("diff:"+secdiff);-->
<!--            }-->
<!--            else-->
<!--            {-->
<!--              map.panTo(point);-->
<!--              var marker = new GMarker(point);-->
<!--              map.addOverlay(marker);-->
<!--              var newtime2=new Date();-->
<!--              var secdiff2=oldtime.dateDiff("s",newtime2);-->
<!--              alert("diff1:"+secdiff);-->
<!--              alert("diff2:"+secdiff2);-->
<!--              alert("lat:"+point.)-->
<!--            }-->
<!--          });-->
<!--    }-->

<!--    function load() {-->
<!--      if (GBrowserIsCompatible()) {  -->
<!--        map = new GMap2(document.getElementById("map"));-->
<!--        map.addControl(new GSmallMapControl());-->
<!--        map.addControl(new GMapTypeControl());-->
<!--        -->
<!--        var geoPoint= new GLatLng(39.92, -116.46);-->
<!--        -->
<!--        map.setCenter(geoPoint, 4);-->
<!--        -->
<!--        GEvent.addListener(map, "click", function()-->
<!--                    {-->
<!--                      locate(prompt("请输入需要查询的地址", ""));-->
<!--                    });-->
<!--      }-->
<!--    }-->
    
		function initialize() {
			geocoder = new google.maps.Geocoder();
			var latlng = new google.maps.LatLng(-34.397, 150.644);
			var myOptions = {       
					zoom: 8,
					center: latlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP
					};
			map = new google.maps.Map(document.getElementById("map1"), myOptions);
	        google.maps.event.addListener(map, "click", function()
	        		{
	        	locate(prompt("请输入需要查询的地址", ""));
	        	});
	        }  
	    //地理编码查询地点经纬度 V3版
	    function locate(address)
	    {
	        	//alert(address);        
	        	//var oldtime=new Date();
	            geocoder.geocode( { 'address': address}, function(results, status) {
	              if (status == google.maps.GeocoderStatus.OK) {
	            	  //alert(status);
	              	//var newtime=new Date();
	            	//var secdiff=oldtime.dateDiff("s",newtime);
	                //alert("diff1:"+secdiff+";"+results[0].geometry.location);
	            	map.setCenter(results[0].geometry.location);
	                var marker = new google.maps.Marker({
	                	map: map,
	                	position: results[0].geometry.location
	                	});
	                } else {
	                	alert("Geocode was not successful for the following reason: " + status);
	                }
	              });
	        }
    //]]>
    </script>
  </head>
  <!--加载时调用load()函数加载地图，注意加上onunload="GUnload()"防止内存泄露-->
  <body onload="initialize()">
  <div>    <input type="button" value="Geocode" onclick="locate(prompt('请输入需要查询的地址', ''))"></div>
    <!--以下id为map的DIV元素即为Google地图的容器-->
    <div id="map1" style="height:80%;top:30px"></div>
  </body>
</html>
